package com.ocom.aiotweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.aiotweb.feignclient.AiotClientService;
import com.ocom.aiotweb.service.ConsumeUpgradePageService;
import com.ocom.aiotweb.vo.accessupgradepage.AccessUpgradePageOnShowVo;
import com.ocom.aiotweb.vo.consumeupgradepage.ConsumeUpgradePageOnShowVo;
import com.ocom.aiotweb.vo.consumeupgradepage.ImportUpgradeVo;
import com.ocom.aiotweb.vo.consumeupgradepage.OnShowVo;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.aiot.EntityMjAppVerInfo;
import com.ocom.common.entity.aiot.EntityXfAppVerInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConsumeUpgradePageServiceImpl implements ConsumeUpgradePageService {

    @Autowired
    private AiotClientService aiotClientService;

    // 消费升级文件列表（ConsumeUpgradePage/onShow）
    @Override
    public Result ConsumeUpgradePageOnShow(OnShowVo onShowVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_fileType(),onShowVo.getFileType());
        map.put(mapKey.getKey_status(),0);
        map.put(mapKey.getKey_page(), onShowVo.getPage());
        map.put(mapKey.getKey_size(), onShowVo.getSize());
        Result result = aiotClientService.GetDeviceVerInfo(map);
        if (result.getCode()!= 200){
            return ResultUtil.error(result.getCode());
        }
        ConsumeUpgradePageOnShowVo consumeUpgradePageOnShowVo = JSON.parseObject(JSON.toJSONString(result.getData()), ConsumeUpgradePageOnShowVo.class);
        ArrayList<Map> list1 = new ArrayList<>();
        if (consumeUpgradePageOnShowVo != null){
            List<EntityXfAppVerInfo> list = consumeUpgradePageOnShowVo.getList();
            for (EntityXfAppVerInfo entityXfAppVerInfo: list) {
                HashMap<Object, Object> map1 = new HashMap<>();
                map1.put("fileId",entityXfAppVerInfo.getId());
                map1.put(mapKey.getKey_fileType(),entityXfAppVerInfo.getApkType());
                map1.put(mapKey.getKey_newAppVer(),entityXfAppVerInfo.getAppVer());
                map1.put(mapKey.getKey_oldAppVer(),entityXfAppVerInfo.getOldAppVer());
                list1.add(map1);
            }

        }
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("total",consumeUpgradePageOnShowVo.getTotal());
        map1.put("list",list1);
        return ResultUtil.success(map1);
    }

    /**
     * 文件上传（UpgradePage/importUpgrade）
     * */
    @Override
    public Result ConsumeUpgradePageImportUpgrade(ImportUpgradeVo importUpgradeVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_apkType(),importUpgradeVo.getFileType());
        map.put(mapKey.getKey_appVer(),importUpgradeVo.getNewAppVer());
        map.put(mapKey.getKey_oldAppVer(),importUpgradeVo.getOldAppVer());
        map.put(mapKey.getKey_apkFileUrl(),importUpgradeVo.getFileUrl());
        map.put(mapKey.getKey_status(),0);
        return aiotClientService.AddDeviceVerInfo(map);
    }

    //2.5.3升级文件删除（ConsumeUpgradePage/delete）
    @Override
    public Result ConsumeUpgradePageDelete(Integer id) {
        return aiotClientService.DeleteDeviceVerInfo(id);
    }

}
